package com.tensquare.user.controller;

import com.tensquare.user.pojo.User;
import com.tensquare.user.service.UserService;
import entity.Result;
import entity.StatusCode;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwt;
import net.bytebuddy.asm.Advice;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import util.JwtUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by WF on 2020/4/2 15:41
 */
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private JwtUtil jwtUtil;
    @Autowired
    private HttpServletRequest request;
    //1.注册用户
    @PostMapping("register/{code}")
    public Result register(@PathVariable String code, @RequestBody User user){
        //1.1)注册新用户
        boolean b = userService.register(code,user);
        if(b) {
            //将用户信息放到token中
            String token = jwtUtil.createJWT(user.getId(), user.getNickname(), "user");
            return new Result(true, StatusCode.OK, "注册成功！");
        }
        return new Result(false, StatusCode.ERROR, "注册失败！");
    }
    //2.根据手机号发送手机验证码
    @PostMapping("sendsms/{mobile}")
    public Result sendSms(@PathVariable String mobile){
        //2.1)发送手机验证码
        userService.sendSms(mobile);
        //2.2)返回
        return new Result(true,StatusCode.OK,"发送手机验证码成功！");
    }
    //3.用户登录
    @PostMapping("login")
    public Result login(@RequestBody User user){
        //3.1)根据当前的用户手机查询是否存在用户，如果存在，就进行密码比对，如果一致，登录成功！
        User loginUser = userService.login(user);
        if(loginUser != null) {
            //将用户信息放到token中
            String token = jwtUtil.createJWT(user.getId(), user.getNickname(), "user");
            if(StringUtils.isNotBlank(token)){
                Map map = new HashMap<>();
                map.put("token",token);
                map.put("nickname",user.getNickname());
                return new Result(true, StatusCode.OK, "登录成功！",map);
            }
        }
        return new Result(false,StatusCode.ERROR,"登录失败！");
    }
    //4.删除用户
    @DeleteMapping("{userId}")
    public Result delete(@PathVariable String userId){
        //4.1)从请求作用域中得到claims对象
        Claims claims = (Claims) request.getAttribute("claims");
        //4.2)判断是否存在，如果存在，就得到roles
        if(claims != null){
            String roles = (String) claims.get("roles");
            if(StringUtils.isNotBlank(roles) && roles.equals("admin")){
                userService.delete(userId);
                return new Result(true, StatusCode.OK, "删除用户成功！");
            }
        }
        return new Result(false, StatusCode.ERROR, "删除用户失败！");
    }
    //5.修改粉丝数
    @PostMapping("/incfans/{userid}/{x}")
    public Result incfans(@PathVariable String userid,@PathVariable int x){
         userService.incFans(userid,x);
         return new Result(true,StatusCode.OK,"修改粉丝数成功！");
    }
    //6.修改关注数
    @PostMapping("/incfollow/{userid}/{x}")
    public Result incfollow(@PathVariable String userid,@PathVariable int x){
         userService.incFollow(userid,x);
        return new Result(true,StatusCode.OK,"修改关注数成功！");
    }
}
